/**
* \brief 
* \author pengcheng (pengcheng@yslrpch@126.com)
* \date 2020-05-23
* \attention CopyrightÃ‚Â©ADC Technology(tianjin)Co.Ltd
* \attention Refer to COPYRIGHT.txt for complete terms of copyright notice
*/
#include <cmw_app/work.h>

namespace adc
{

Worker::Worker(float hz):
        timer_(hz),
        worker_state_(WS_INIT)
{
    main_thread_ = std::thread(&Worker::MainThread, this);
}
Worker::~Worker()
{
    worker_state_ = WS_TERMINATE;
    main_thread_.join();
}

void Worker::Run()
{
    worker_state_ = WS_RUN;
}

void Worker::Suspend()
{
    worker_state_ = WS_SUSPEND;
}

void Worker::Terminate()
{
    worker_state_ = WS_TERMINATE;
}

void Worker::MainThread()
{
    while (true)
    {
        timer_.Start();
        if (WS_RUN == worker_state_)
        {
            DoWork();
        }
        else if (WS_INIT == worker_state_ || WS_SUSPEND == worker_state_)
        {
            continue;
        }
        else if (WS_TERMINATE == worker_state_)
        {
            break;
        }
        else
        {
            std::cerr<< "worker state is absolutely wrong."<<std::endl;
        }

        timer_.Stop();
    }
}
}